package com.example.gfsquadwebviewproject.util;

import android.content.Context;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class DeviceUtils {
    String TAG = "DeviceUtils";
    Context context;

    public DeviceUtils(Context context) {
        this.context = context;
    }

    /**
     * 获取设备的id
     * @return
     */
    public String getDeviceId(){
//        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        String deviceId = telephonyManager.getDeviceId();
//        if (deviceId==null){
//            //android.provider.Settings;
//            deviceId= Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//        }
//        return deviceId;
        return "";
    }

    //获取手机MAC
    public String getMac(){
        String MAC=null;
        try {
            if (PreferencesUtils.getString(context, "MAC") == null) {
                MAC = getLocalMacAddressFromIp();
                PreferencesUtils.putString(context, "MAC", MAC);
            } else {
                MAC = PreferencesUtils.getString(context, "MAC");
            }
        }
        catch (Exception e){
            L.d(TAG,"getMAC error："+e);
        }
        return MAC;
    }

    /**
     * 根据IP地址获取MAC地址
     * @return
     */
    private static String getLocalMacAddressFromIp() {
        String strMacAddr = null;
        try {
            //获得IpD地址
            InetAddress ip = getLocalInetAddress();
            byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress();
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                if (i != 0) {
                    buffer.append(':');
                }
                String str = Integer.toHexString(b[i] & 0xFF);
                buffer.append(str.length() == 1 ? 0 + str : str);
            }
            strMacAddr = buffer.toString().toUpperCase();
        } catch (Exception e) {
        }
        return strMacAddr;
    }

    /**
     * 获取移动设备本地IP
     * @return
     */
    private static InetAddress getLocalInetAddress() {
        InetAddress ip = null;
        try {
            //列举
            Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
            while (en_netInterface.hasMoreElements()) {//是否还有元素
                NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一个元素
                Enumeration<InetAddress> en_ip = ni.getInetAddresses();//得到一个ip地址的列举
                while (en_ip.hasMoreElements()) {
                    ip = en_ip.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                        break;
                    else
                        ip = null;
                }
                if (ip != null) {
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ip;
    }


}
